package com.zhao.fishingguideserver.controller;

import com.zhao.fishingguideserver.POJO.Result;
import com.zhao.fishingguideserver.service.FishService;
import com.zhao.fishingguideserver.utils.FishImageSqlGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 鱼类图片SQL生成控制器
 */
@RestController
@RequestMapping("/fish-sql")
@Slf4j
public class FishImageSqlController {
    
    @Autowired
    private FishImageSqlGenerator sqlGenerator;
    
    @Autowired
    private FishService fishService;
    
    /**
     * 生成鱼类图片更新SQL
     */
    @PostMapping("/generate")
    public Result<Map<String, Object>> generateSql(@RequestBody Map<String, Object> request) {
        try {
            String ossDomain = (String) request.get("ossDomain");
            String folderPath = (String) request.get("folderPath");
            @SuppressWarnings("unchecked")
            List<String> fishNames = (List<String>) request.get("fishNames");
            
            if (ossDomain == null || folderPath == null) {
                return Result.error("OSS域名和文件夹路径不能为空");
            }
            
            if (fishNames == null || fishNames.isEmpty()) {
                fishNames = getAllFishNamesFromDatabase();
            }
            
            List<String> sqlList = sqlGenerator.generateUpdateSql(ossDomain, folderPath, fishNames);
            String completeScript = sqlGenerator.generateCompleteSqlScript(ossDomain, folderPath, fishNames);
            
            Map<String, Object> result = Map.of(
                "sqlList", sqlList,
                "completeScript", completeScript,
                "totalCount", sqlList.size(),
                "ossDomain", ossDomain,
                "folderPath", folderPath
            );
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("生成SQL失败", e);
            return Result.error("生成SQL失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取所有鱼类名称
     */
    @GetMapping("/fish-names")
    public Result<List<String>> getAllFishNames() {
        try {
            List<String> fishNames = getAllFishNamesFromDatabase();
            return Result.success(fishNames);
        } catch (Exception e) {
            log.error("获取鱼类名称失败", e);
            return Result.error("获取鱼类名称失败: " + e.getMessage());
        }
    }
    
    /**
     * 快速生成SQL（使用默认配置）
     */
    @PostMapping("/quick-generate")
    public Result<Map<String, Object>> quickGenerateSql(@RequestParam String ossDomain, 
                                                       @RequestParam(defaultValue = "") String folderPath) {
        try {
            List<String> fishNames = getAllFishNamesFromDatabase();
            List<String> sqlList = sqlGenerator.generateUpdateSql(ossDomain, folderPath, fishNames);
            String completeScript = sqlGenerator.generateCompleteSqlScript(ossDomain, folderPath, fishNames);
            
            Map<String, Object> result = Map.of(
                "sqlList", sqlList,
                "completeScript", completeScript,
                "totalCount", sqlList.size(),
                "ossDomain", ossDomain,
                "folderPath", folderPath
            );
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("快速生成SQL失败", e);
            return Result.error("快速生成SQL失败: " + e.getMessage());
        }
    }
    
    /**
     * 从数据库获取所有鱼类名称
     */
    private List<String> getAllFishNamesFromDatabase() {
        return fishService.selectAll().stream()
                .map(fish -> fish.getSpecies_name())
                .collect(Collectors.toList());
    }
}
